/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Supporter;

import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * User must call method init(File logFileName) before using.
 * User must call method close() before exit the application.
 * @author Ivan
 */
public class OurLogger {

    private static final Logger logger = Logger.getLogger("WebServer");

    public static boolean init(File logFileName) {
        try {
            logger.addHandler(new FileHandler(logFileName.getAbsolutePath(), false));
        } catch (IOException ex) {
            Logger.getLogger(OurSupporter.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (SecurityException ex) {
            Logger.getLogger(OurSupporter.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        
        return true;
    }

    public static void logError(Object invoker, String funtionName, Exception ex, Level level) {
        logger.log(level, invoker.toString() + " - " + funtionName, ex);
    }

    public static void logNotError(Object invoker, String message, Level level) {
        logger.log(level, "{0}: {1}", new Object[]{invoker.toString(), message});
    }

    public static void close() {
        if (logger.getHandlers().length == 1) {
            logger.getHandlers()[0].close();
        }
    }
}
